package com.asiainfo.dacp.datastash.broker.leader.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.asiainfo.dacp.datastash.broker.StashBrokerBoot;
import com.asiainfo.dacp.datastash.broker.core.enums.UnitStateEnum;
import com.asiainfo.dacp.datastash.broker.facade.service.UnitLoaderService;
import com.asiainfo.dacp.datastash.broker.leader.domain.OperEnum;
import com.asiainfo.dacp.datastash.orm.dao.inter.DataStashUnitInterDao;
import com.asiainfo.dacp.datastash.orm.dao.state.DataStashUnitStateLogDao;
import com.asiainfo.dacp.datastash.orm.po.inter.DataStashUnitInterPo;
import com.asiainfo.dacp.datastash.orm.po.inter.pk.DataStashUnitInterPoPK;
import com.asiainfo.dacp.datastash.orm.po.state.DataStashUnitStateLogPo;
import com.google.common.base.Strings;

@Service
public class InterService {

	private static Logger LOG = LoggerFactory.getLogger(InterService.class);

	@Autowired
	private DataStashUnitStateLogDao unitStateDao;

	@Autowired
	private DataStashUnitInterDao interDao;

	@Autowired
	private UnitLoaderService unitLoaderService;

	public void service() {

		List<DataStashUnitInterPo> inters = interDao.findAll();

		for (DataStashUnitInterPo inter : inters) {
			DataStashUnitInterPoPK id = inter.getDataStashUnitInterPoPK();
			String oper = id.getOper();

			String unitProcId = id.getUnitProcId();

			DataStashUnitStateLogPo one = unitStateDao.findOne(unitProcId);
			if (one == null) {
				LOG.warn(String.format("前台操作: 流水 [%s] 在unit状态表中记录不存在. ", unitProcId));
				continue;
			}

			// 检查是否重复操作
			if (OperEnum.FORCE_PASS.value().equalsIgnoreCase(oper)) {// 强制通过
				LOG.info(String.format("前台操作: 流水 [%s] 准备执行 [强制通过] 操作 .", unitProcId));
				String isOver = one.getIsOver();

				if ("1".equals(isOver)) { // 运行
					LOG.info(String.format("前台操作: 流水 [%s] 开始执行 [强制通过] 操作 .", unitProcId));
					forcePass(unitProcId);
				} else {
					LOG.warn(String.format("前台操作: 流水 [%s] [强制通过] 操作重复执行，忽略.", unitProcId));
				}
			} else if (OperEnum.REDO.value().equalsIgnoreCase(oper)) {// 重新执行
				LOG.info(String.format("前台操作: 流水 [%s] 准备执行 [重新执行] 操作 .", unitProcId));
				List<DataStashUnitStateLogPo> runnings = unitStateDao.findUnitStateNonOver(one.getUnitCode(),
						one.getBatchNo());
				//TODO 扩展一个字段 用于区分类型
				if (runnings != null && runnings.size() > 0) {
					LOG.warn(String.format("前台操作: 流水 [%s] [重新执行] 操作重复执行，忽略.", unitProcId));
				} else {
					LOG.info(String.format("前台操作: 流水 [%s] 开始执行 [重新执行] 操作 .", unitProcId));
					redo(unitProcId);
				}
			}
		}
		interDao.deleteInBatch(inters);
	}

	public void redo(String unitProcId) {
		//unitStateDao.updateOver(unitProcId);
		// create new
		// 如果其他状态（含失败）－翻牌， 如果成功－不翻牌
		DataStashUnitStateLogPo unitStatePo = unitStateDao.findOne(unitProcId);
		if (unitStatePo != null) {
			String state = unitStatePo.getState();
			boolean isTurn = true;
			if (UnitStateEnum.SUCESS.equals(state)) {
				isTurn = false;
			}
			unitLoaderService.createStashUnit(unitStatePo.getUnitCode(), unitStatePo.getBatchNo(), isTurn);
		}
	}

	public void forcePass(String unitProcId) {
		unitStateDao.updateOver(unitProcId);
	}

}
